package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.util.DateUtils;
import com.youlai.system.converter.AssetComputerConverter;
import com.youlai.system.mapper.AssetComputerMapper;
import com.youlai.system.mapper.SysUserMapper;
import com.youlai.system.model.bo.AssetComputerBO;
import com.youlai.system.model.entity.AssetComputer;
import com.youlai.system.model.entity.SysUser;
import com.youlai.system.model.form.ComputerForm;
import com.youlai.system.model.query.ComputerPageQuery;
import com.youlai.system.model.query.UserPageQuery;
import com.youlai.system.model.vo.AssetComputerImportVO;
import com.youlai.system.model.vo.AssetComputerPageVO;
import com.youlai.system.service.AssetComputerService;
import com.youlai.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class AssetComputerServiceImpl extends ServiceImpl<AssetComputerMapper, AssetComputer> implements AssetComputerService {

    private final SysUserMapper sysUserMapper;
    private final SysUserService userService;
    private final AssetComputerConverter assetComputerConverter;
    @Override
    public Page<AssetComputerPageVO> listPagedComputers(ComputerPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();

        Page<AssetComputerBO> page = new Page<>(pageNum, pageSize);

        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
        // 查询数据
        Page<AssetComputerBO> computerPage = this.baseMapper.listPagedComputer(page, queryParams);
        // 转换bo对象
        return  assetComputerConverter.bo2PageVo(computerPage);
    }

    @Override
    public Long saveComputer(ComputerForm computerForm) {
        String tag = computerForm.getTag();

        long count = this.count(new LambdaQueryWrapper<AssetComputer>().eq(AssetComputer::getTag, tag));
        Assert.isTrue(count == 0, "标签已经存在");
        // form->entity
        AssetComputer assetComputer = assetComputerConverter.form2entity(computerForm);
        // 保存部门并返回部门ID
        boolean result = this.save(assetComputer);
        return assetComputer.getId();
    }

    @Override
    public ComputerForm getComputerFormData(Long computerId) {
        return this.baseMapper.getComputerFormData(computerId);
    }

    @Override
    public boolean updateComputer(Long computerId, ComputerForm computerForm) {
        String tag = computerForm.getTag();
        long count = this.count(new LambdaQueryWrapper<AssetComputer>()
                .eq(AssetComputer::getTag, tag)
                .ne(AssetComputer::getId, computerId)
        );
        Assert.isTrue(count == 0, "标签已存在");
        // form -> entity
        AssetComputer assetComputer = assetComputerConverter.form2entity(computerForm);
        return  this.updateById(assetComputer);
    }

    @Override
    public boolean deleteComputer(String idStr) {
        Assert.isTrue(StrUtil.isNotBlank(idStr), "删除的电脑数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public List<AssetComputerImportVO> listExportComputers(ComputerPageQuery queryParams) {
        return this.baseMapper.listExportComputers(queryParams);
    }
}
